package com.qiuzhiyi.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.Const;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.ZhaoGongAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.ZhaoGongItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2018/1/22 0022
 * Description: 搜索结果
 */

public class SearchListActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;

    private ZhaoGongAdapter mAdapter;
    private List<ZhaoGongItem.ListBean> mZhaoGongItemList;
    private String mName;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_list;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mName=getIntent().getStringExtra("name");
        mTopNavigation.setNavigationBarListener(this);
        setSmartRefreshLayout(mRefreshLayout);

        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));

        mAdapter = new ZhaoGongAdapter(R.layout.item_zhaogong, "1");
        mAdapter.bindToRecyclerView(mRecycler);
        mAdapter.setEmptyView(R.layout.layout_empty_search);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "1");
                bundle.putString("id", String.valueOf(mZhaoGongItemList.get(position).getCId()));
                bundle.putString("salary",  mZhaoGongItemList.get(position).getCCapital());
                bundle.putString("name", mZhaoGongItemList.get(position).getCName());
                startWithArg(JobDetailActivity.class, bundle);
            }
        });


        mAdapter.setNewData(mZhaoGongItemList);
        showList("1",mName);
    }
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        showList("1",mName);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        showList(String.valueOf(mPage + 1),mName);
    }
    private void showList(String page,String name) {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", page);
        map.put("numPerPage", Const.NUM_PER_PAGE);
        map.put("jobType","1");
        map.put("cName",name);
        ObservableHelper.createObservable(RetrofitHelper.createService().showZhaogongList(map))
                .subscribe(new SelectorObserver<BaseItem<ZhaoGongItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onSuccess(BaseItem<ZhaoGongItem> item) {
                        stopRefreshLoad();
                        List<ZhaoGongItem.ListBean> tmp = item.getObjValue().getList();
                        if (mIsRefresh) {
                            mPage = 1;
                            mZhaoGongItemList = tmp;
                            mAdapter.setNewData(mZhaoGongItemList);
                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mRefreshLayout.setLoadmoreFinished(false);
                            }

                        } else {
                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mPage++;
                                mRefreshLayout.setLoadmoreFinished(false);
                            }
                            mAdapter.addData(tmp);
                        }
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        stopRefreshLoad();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        if (mZhaoGongItemList == null) {
                            showLoadingDialog();
                        }
                    }
                });
    }
}
